package com.dksidana.java.jdbc;

import java.sql.Connection;
import java.sql.SQLException;

import com.dksidana.java.jdbc.core.QueryRunner;
import com.dksidana.java.jdbc.core.ResultSetHandler;
import com.dksidana.java.pool.Pool;

public class ConnectionPoolWrapper implements QueryRunner {
	private final Pool<Connection, SQLException> connPool=null;

	public <T> T executeQuery(String query, ResultSetHandler<T> rsh) throws SQLException {
		Connection connection=null;
		try {
			connection=connPool.borrowObject();
			return JDBCUtils.executeQuery(connection, query, rsh);
		} catch (SQLException e) {
			try {
				connPool.invalidateObject(connection);
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
			e.printStackTrace();
		}
		finally
		{
			try {
				connPool.returnObject(connection);
			} catch (SQLException e) {

				e.printStackTrace();
			}
		}
		return null;
	}


}
